import Vue from 'vue';
import user from '../../core/user.js';
import request from '../../core/request.js';

const state = {
	accessToken: null,
	info: null,
	showLoginModal: false,
	tempParentId: 0,
	targoodsid: 0,
	useridres: 0,
	mallid: 1,
	agent_distrid: 0, // 代理id
};

const getters = {
	agent_distrid(state) {
		return state.agent_distrid;
	},
	accessToken(state) {
		return state.accessToken;
	},
	info(state) {
		return state.info;
	},
	showLoginModal(state) {
		return state.showLoginModal;
	},
	tempParentId(state) {
		return state.tempParentId;
	},
	// 商品真分享
	targoodsid(state) {
		return state.targoodsid;
	},
	useridres(state) {
		return state.useridres;
	},
	mallid(state) {
		return state.mallid;
	},

	is_vip(state) {
		return state.is_vip_card_user;
	}
};

const mutations = {
	agent_distrid(state, data) {
		state.agent_distrid = data;
	},
	accessToken(state, data) {
		state.accessToken = data;
	},
	info(state, data) {
		state.info = data;
	},
	showLoginModal(state, data) {
		state.showLoginModal = data;
	},
	tempParentId(state, data) {
		state.tempParentId = data;
	},
	// 商品真分享
	targoodsid(state, data) {
		state.targoodsid = data;
	},
	useridres(state, data) {
		state.useridres = data;
	},
	mallid(state, data) {
		state.mallid = data;
	},
};

const actions = {
	agent_distrid(context, agent_id) {
		context.commit('agent_distrid', agent_id);
	},
	accessToken(context) {
		if (!user.isLogin()) {
			context.commit('accessToken', null);
		}
		user.getAccessToken().then(accessToken => {
			context.commit('accessToken', accessToken);
		});
	},
	info(context, options) {
		if (!user.isLogin()) {
			context.commit('accessToken', null);
		}
		user.getAccessToken().then(accessToken => {
			context.commit('accessToken', accessToken);
			user.getInfo(options).then(data => {
				context.commit('info', data);
			});
		});
	},
	refreshInfo(context) {
		context.commit('accessToken', null);
	},
	refresh(context) {
		if (!user.isLogin()) {
			context.commit('accessToken', null);
		}
		user.getAccessToken().then(accessToken => {
			context.commit('accessToken', accessToken);
			user.getInfo({
				refresh: true,
			}).then(data => {
				context.commit('info', data);
			}).catch(e => {});
		}).catch(e => {});
	},
	setTempParentId(context, data) {
		context.commit('tempParentId', data);
	},
	loadAccessTokenFormCache(context) {
		if (context.accessToken) return;
		user.getAccessToken({
			cacheOnly: true,
		}).then(e => {
			if (!e) return;
			context.commit('accessToken', e);
		});
	},
	logout(context) {
		context.commit('accessToken', null);
		user.loginByToken(null);
	},
	// 商品真分享
	goodshare(context, options) {
		// console.log(context, options, 'zhehsihsnag************');
		context.commit('targoodsid', options.targoodsid);
		context.commit('useridres', options.useridres);
		context.commit('mallid', options.mallid);
		if (!user.isLogin()) {
			context.commit('accessToken', null);
		}
		user.getAccessToken().then(accessToken => {
			context.commit('accessToken', accessToken);
			user.getInfo({
				refresh: true,
			}).then(data => {
				let mallurl = Vue.prototype.$siteInfo.apiroot + '?_mall_id=' + options.mallid +
					'&r='
				let url = Vue.prototype.$api.gold.index
				let pdata = {
					type: 5,
					targetid: options.targoodsid,
					userid: options.useridres
				}
				if (options.mallid > 1) {
					url = mallurl + Vue.prototype.$apiUrl.gold.index
				}
				request({
					url: url,
					data: pdata
				}).then(res => {
					if (res.data.isdone >= 0) {

					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				});
			}).catch(e => {});
			// context.commit('targoodsid', options.targoodsid);
			// context.commit('useridres', options.useridres);
			// context.commit('mallid', options.mallid);
		});
	},
};

export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions
}
